package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.TbContentCategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentCategory")
public class ContentCategoryController {

    @DubboReference
    private TbContentCategoryService tbContentCategoryService;

    @RequestMapping("tocontentCategory")
    public String toString() {
        return "admin/content_category";
    }

    @RequestMapping("show")
    @ResponseBody
    public Result show(TbContentCategory tbContentCategory){
        List<TbContentCategory> tbContentCategories = tbContentCategoryService.selectAll(tbContentCategory);
        if (tbContentCategories == null) {
            return Result.response(ResultEnum.ERROR);
        }
        return Result.response(ResultEnum.SUCCESS).setData(tbContentCategories);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbContentCategory tbContentCategory){
        boolean b = tbContentCategoryService.insert(tbContentCategory);
        if (b) {
            return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);
    }

    @RequestMapping("toupdate")
    @ResponseBody
    public Result toupdate(Long id){
        TbContentCategory tbContentCategory = tbContentCategoryService.selectById(id);
        if (tbContentCategory == null) {
            return Result.response(ResultEnum.ERROR);
        }
        return Result.response(ResultEnum.SUCCESS).setData(tbContentCategory);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbContentCategory tbContentCategory){
        boolean b = tbContentCategoryService.updateById(tbContentCategory);
        if (b) {
            return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);
    }

    @RequestMapping("deleteCon")
    @ResponseBody
    public Result deleteCon(@RequestParam("ids") Integer[] ids){
        boolean b = tbContentCategoryService.deleteByIds(ids);
        if (b) {
            return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);

    }
}
